$(document).ready(function () {

    init_page();

    /* 左侧轮播 */
    layui.use(['carousel', 'form'], function () {
        var carousel = layui.carousel
        carousel.render({
            elem: '#carousel'
            , width: '35%'
            , height: '100%'
            , anim: 'fade'
            , autoplay: true
            , arrow: 'none'
            , indicator: 'none'
            , interval: 5000
        });
    });

    /* Sigin up按钮点击事件 */
    $("#join-submit").on("click", function () {
        $.ajax({
            url: "/user/join",
            method: "post",
            data: $("#join-form").serialize()
        }).then(function (data) {
            var status = $(data).find("error_code").text();
            switch (status) {
                case "0":
                    window.location.href = "/pages/index.html";
                    break;

                default:
                    alert("Username already exists!");
                    break;
            }
        });
    });

});

/* 初始化页面, 获取轮播图 */
function init_page() {
    $.ajax({
        url: "/init/join",
        method: "get",
        success: function (data) {
            var images = "";
            var img = $(data).find("url");
           for (var i = 0; i < img.length; i++)
            {
                var url = img.eq(i).text();
                images += "<div><img src=\"" + url + "\"></div>";
            }
            $("#carousel-img").append(images);
        }
    });
}